// Created by Windward Studios - no copyright is claimed. This code can be used in
// any manner by anyone for any reason. There is no copyright of any kind on it. You may
// use it in commercial products. You may change it without sharing those changes.
// We ask that you keep the "created by Windward Studios" in a comment at the top.

using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace net.windward.utils.ado.OleDb
{
	/// <summary>
	/// Represents an OleDb server.
	/// </summary>
	public class WrOleDbServer : IWrServer
	{
		private readonly DbProviderFactory provider;
		private readonly string odbcProvider;
		private readonly string name;

		/// <summary>
		/// Create a OleDb server object.
		/// </summary>
		/// <param name="provider">The provider for this server.</param>
		/// <param name="name">The name of the server.</param>
		/// <param name="odbcProvider">The provider for this connection.</param>
		public WrOleDbServer(DbProviderFactory provider, string odbcProvider, string name)
		{
			this.provider = provider;
			this.odbcProvider = odbcProvider;
			this.name = name;
		}

		/// <summary>
		/// The name of the server.
		/// </summary>
		public string Name
		{
			get { return name; }
		}

		#region IWrServer Members

		/// <summary>
		/// All databases on this server. OleDb cannot enum databases and always returns an empty array.
		/// </summary>
		/// <param name="credentials">The user credentials to access the database.</param>
		/// <returns>All databases on this server.</returns>
		public IWrDatabase[] GetDatabases(WrCredentials credentials)
		{
			return GetDatabases(ConnectionString(credentials));
		}

		/// <summary>
		/// All databases on this server. OleDb cannot enum databases and always returns an empty array.
		/// </summary>
		/// <param name="connectionString">The connection string to access the database.</param>
		/// <returns>All databases on this server.</returns>
		public IWrDatabase[] GetDatabases(string connectionString)
		{
			ArrayList rtn = new ArrayList();
			using (OleDbConnection conn = new OleDbConnection(connectionString))
			{
				conn.Open();
				DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Catalogs, new object[] {null});
				foreach (DataRow rowOn in schemaTable.Rows)
					rtn.Add(new WrOleDbDatabase(provider, odbcProvider, name, (string)rowOn["CATALOG_NAME"]));
			}
			return (WrOleDbDatabase[]) rtn.ToArray(typeof (WrOleDbDatabase));
		}

		#endregion

		/// <summary>
		/// Create a database object.
		/// </summary>
		/// <param name="database">The name of the database.</param>
		public IWrDatabase CreateDatabase(string database)
		{
			return new WrOleDbDatabase(provider, odbcProvider, name, database);
		}

		private string ConnectionString(WrCredentials credentials)
		{
			DbConnectionStringBuilder connStrBuilder = provider.CreateConnectionStringBuilder();
			connStrBuilder.Add("Provider", odbcProvider);
			connStrBuilder.Add("Data Source", name);
			if (!credentials.UseWindowsIdentity)
			{
				connStrBuilder.Add("User Id", credentials.Username);
				connStrBuilder.Add("Password", credentials.Password);
			}
			return connStrBuilder.ConnectionString;
		}

		/// <summary>
		/// Compares the current instance with another object of the same type.
		/// </summary>
		/// <param name="obj">An object to compare with this instance. </param>
		/// <returns>a 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
		public int CompareTo(object obj)
		{
			WrOleDbServer db = obj as WrOleDbServer;
			if (db == null)
				return 1;
			return name.CompareTo(db.name);
		}
	}
}
